<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;


class ReportMapController extends Controller
{

    public function reportMap(Request $request)
    {

        $user_info = DB::table('engineering')->where('deleted_at',null)->distinct()->get('province_name')->map(function ($value) {return (array)$value;})->toArray();

        $resurt = [];
        foreach ($user_info as $key => $value){
            $resurt[$key]['name'] = $value['province_name'];
            $resurt[$key]['value'] = DB::table('engineering')->where('province_name',$value['province_name'])->count();
        }

        return $resurt;
    }

    public function reportTotal(Request $request)
    {

        $resurt = DB::table('engineering')->where('deleted_at',null)->distinct()->get('engineering_name')->map(function ($value) {return (array)$value;})->toArray();

        return ['total'=>count($resurt)];
    }

    public function reportEngineering(Request $request)
    {

        $resurt = DB::table('engineering')->where('deleted_at',null)->distinct()->get()->map(function ($value) {return (array)$value;})->toArray();

        return $resurt;
    }

    public function reportWorkUserTotal(Request $request)
    {

        $resurt = DB::table('work_user')->select('id_card')->where('deleted_at',null)->where('status',1)->where('misdeed', 1)->distinct()->count();

        return ['total'=>$resurt];
    }

    public function reportTeamTotal(Request $request)
    {

        $resurt = DB::table('team')->where('deleted_at',null)->where('status',1)->count();

        return ['total'=>$resurt];
    }




    public function reportEngineeringList(Request $request)
    {

        $resurt = DB::table('engineering')->select('engineering_name','id','engineering_name as engineering')->where('deleted_at',null)->distinct()->get()->map(function ($value) {return (array)$value;})->toArray();

        $resurt[] = ['id'=>0,'engineering_name'=>'全部','engineering'=>'全部'];

        sort($resurt);

        return $resurt;
    }


    public function reportWorkUserInfo(Request $request)
    {

        $data = $request->all();
        $engineering = $data['engineering'] ?? null;
        $status = $data['status'] ?? null;

        Log::info('大屏请求：'.$engineering);

        $now = Carbon::now();
        $twelveMonthsAgo = $now->subMonth(12);

        $start = $twelveMonthsAgo->toDateTimeString();
        $end = Carbon::now();

//        var_dump($start);
//        var_dump($end);

        $res = [];

        if($engineering == "全部"){
            $engineering = "";
        }

        if(empty($engineering)){
            if($status == 1){
                $res['subjects'] = DB::table('work_user_history')
                    ->where(function ($query) use ($start,$end) {
                        $query->whereBetween('enter_time',[$start,$end])
                            ->orWhereBetween('exit_time',[$start,$end]);
                    })
                    ->where('deleted_at',null)
                    ->whereIn('type',[1,3])
                    ->select('enter_time','type', DB::raw('count(*) as num'))
                    ->groupBy(DB::raw("YEAR(enter_time), MONTH(enter_time)"))->orderBy('enter_time','asc')->get()->map(function ($value) {return (array)$value;})->toArray();

            }else{
                $res['subjects'] = DB::table('work_user_history')
                    ->where(function ($query) use ($start,$end) {
                        $query->whereBetween('enter_time',[$start,$end])
                            ->orWhereBetween('exit_time',[$start,$end]);
                    })
                    ->where('deleted_at',null)
                    ->where('type',2)
                    ->select('type','exit_time', DB::raw('count(*) as num'))
                    ->groupBy(DB::raw("YEAR(exit_time), MONTH(exit_time)"))->orderBy('exit_time','asc')->get()->map(function ($value) {return (array)$value;})->toArray();

            }

        }else{
            if($status == 1){
                $res['subjects'] = DB::table('work_user_history')
                    ->where(function ($query) use ($start,$end) {
                        $query->whereBetween('enter_time',[$start,$end])
                            ->orWhereBetween('exit_time',[$start,$end]);
                    })
                    ->where('deleted_at',null)
                    ->where('engineering','like','%'.$engineering.'%')
                    ->whereIn('type',[1,3])
                    ->select('enter_time','type','engineering', DB::raw('count(*) as num'))
                    ->groupBy(DB::raw("YEAR(enter_time), MONTH(enter_time)"))->orderBy('enter_time','desc')->get()->map(function ($value) {return (array)$value;})->toArray();
            }else{
                $res['subjects'] = DB::table('work_user_history')
                    ->where(function ($query) use ($start,$end) {
                        $query->whereBetween('enter_time',[$start,$end])
                            ->orWhereBetween('exit_time',[$start,$end]);
                    })
                    ->where('deleted_at',null)
                    ->where('engineering','like','%'.$engineering.'%')
                    ->where('type',2)
                    ->select('type','engineering','exit_time', DB::raw('count(*) as num'))
                    ->groupBy(DB::raw("YEAR(exit_time), MONTH(exit_time)"))->orderBy('exit_time','desc')->get()->map(function ($value) {return (array)$value;})->toArray();
            }

        }
        sort($res['subjects']);

        $total_num = 0;
        foreach ($res['subjects'] as $key => $value){

            if($value['type'] == 1){
                $res['subjects'][$key]['time'] = $value['enter_time'];
            }
            if($value['type'] == 2){
                $res['subjects'][$key]['time'] = $value['exit_time'];
            }
            if($value['type'] == 3){
                $res['subjects'][$key]['time'] = $value['enter_time'];
            }
            if($value['type'] == 4){
                $res['subjects'][$key]['time'] = $value['exit_time'];
            }

            $total_num += $value['num'];

            $res['subjects'][$key]['total_num'] = $total_num;

            if($status == 1){
                $res['subjects'][$key]['month'] = abs(date('m',strtotime($value['enter_time']))).'月';
            }else{
                $res['subjects'][$key]['month'] = abs(date('m',strtotime($value['exit_time']))).'月';
            }


        }


        return $res['subjects'];
    }


}
